//
//  RDDataExcelViewController.m
//  ReverseDiabetes
//
//  Created by 王佩璇 on 2024/8/26.
//

#import "RDDataExcelViewController.h"
#import "YWExcelView.h"
#import "YWRouter.h"



@interface RDDataExcelViewController ()
<YWExcelViewDataSource,YWExcelViewDelegate>
{
    NSString *_ctl;
    NSMutableArray *_list;
}

@property (nonatomic, strong) NSMutableArray *tagArr;
@property (nonatomic, strong) NSMutableArray *dateArr;
@property (nonatomic, strong) NSMutableArray *bloodSugarHistoryArr;

@property (nonatomic, assign) long dateNowtimeInterval;

@property (nonatomic, strong) YWExcelView *exceView;
@end

@implementation RDDataExcelViewController



- (void)viewDidLoad {
    [super viewDidLoad];


    self.dateNowtimeInterval = (long)[[NSDate date] timeIntervalSince1970];

    self.tagArr = [[RDBloodSugarTagDBManager sharedManager] getAllTag];
    self.dateArr = [[NSMutableArray alloc]init];
    self.bloodSugarHistoryArr = [[RDBloodSugarDBManager sharedManager] getBloodSugarHistory];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    YWExcelViewMode *mode = [YWExcelViewMode new];
    mode.style = YWExcelViewStyleheadPlain;
//    mode.headTexts = @[@"类目",@"语文",@"数学",@"物理",@"化学",@"生物",@"英语",@"政治"];
    mode.defalutHeight = 40;
    //推荐使用这样初始化
    YWExcelView *exceView = [[YWExcelView alloc] initWithFrame:CGRectMake(0, STATUS_AND_NAVIGATION_HEIGHT, CGRectGetWidth(self.view.frame), SCREEN_HEIGHT - STATUS_AND_NAVIGATION_HEIGHT) mode:mode];
    exceView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    exceView.dataSource = self;
    exceView.showBorder = YES;
    exceView.delegate = self;
    exceView.loadClickBlock = ^{
        [self loadData];
    };
    [self.view addSubview:exceView];
    
    self.exceView = exceView;
    
    [self loadData];
    
}


- (NSArray *)widthForItemOnExcelView:(YWExcelView *)excelView{
    NSMutableArray *widthArr = [[NSMutableArray alloc]init];
    [widthArr addObject:NumberFloat(100)];
    
    for (int i = 0; i < self.tagArr.count; i++) {
        [widthArr addObject:NumberFloat(60)];
    }
    return widthArr;
}


- (NSInteger)numberOfSectionsInExcelView:(YWExcelView *)excelView{
    return 1;
}
//多少行
- (NSInteger)excelView:(YWExcelView *)excelView numberOfRowsInSection:(NSInteger)section{
    return self.dateArr.count;
}
//多少列
- (NSInteger)itemOfRow:(YWExcelView *)excelView{
    return self.tagArr.count + 1;
//    return 7;
}
- (void)excelView:(YWExcelView *)excelView headView:(UILabel *)label textAtIndexPath:(YWIndexPath *)indexPath{
    if (indexPath.item == 0) {
        label.text = @"日期";
    }else{
        RDBloodSugarTag *model = self.tagArr[indexPath.item-1];
        label.text = model.tag_name;
    }
    
    
}
- (void)excelView:(YWExcelView *)excelView label:(UILabel *)label textAtIndexPath:(YWIndexPath *)indexPath{
    
    if (indexPath.item == 0) {

        if (self.dateArr.count > 0) {
            label.text = self.dateArr[indexPath.row];
        }
        
        
    }else{
        
        RDBloodSugarTag *tagModel = self.tagArr[indexPath.item-1];
        for (RDBloodSugar *model in self.bloodSugarHistoryArr) {
            if (tagModel.tag_id.intValue == model.blood_sugar_tag.intValue && [model.recording_time isEqualToString:self.dateArr[indexPath.row]]) {
                label.text = model.blood_sugar_value;
                break;
            }
        }
    }
    

    
}
-(void)loadData{
    [self getDateArr];
}


-(void)getDateArr{
    
    long day = 60*60*24;
    
    for (int i = 0; i < 30; i++) {
//        self.dateNowtimeInterval
        
        NSDate *detailDate = [NSDate dateWithTimeIntervalSince1970:self.dateNowtimeInterval];
        
//        NSString *yearExc = [detailDate stringWithFormat:@"yyyy"];
//        NSString *monthExc = [detailDate stringWithFormat:@"MM"];
//        NSString *dayExc = [detailDate stringWithFormat:@"dd"];
//        
//        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
//                             @"year", yearExc,
//                             @"month",monthExc,
//                             @"day",dayExc,nil];
        NSString *nowTime = [detailDate stringWithFormat:@"yyyy-MM-dd"];
        [self.dateArr addObject:nowTime];
        
        self.dateNowtimeInterval = self.dateNowtimeInterval - day;
    }
    
    
    [self.exceView reloadData];
    
}

@end
